package com.dmz.workflow.controller.handler;

import com.echronos.commons.Result;
import com.echronos.commons.enums.ErrorMsgEnum;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

import java.util.Objects;

@RestControllerAdvice
public class GlobalExceptionHandler {

    @ExceptionHandler(MethodArgumentNotValidException.class)
    public Result<?> handleMethodArgumentNotValidException(MethodArgumentNotValidException e) {
        return Result.build(ErrorMsgEnum.NORMAL_ERR.getErrorCode(), errorMessageWrapper(e));
    }

    private String errorMessageWrapper(MethodArgumentNotValidException e) {
        return Objects.requireNonNull(e.getBindingResult().getFieldError()).getField() + "字段:" +
                e.getBindingResult().getFieldError().getDefaultMessage();
    }
}
